package com.wc.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

@Slf4j
public class JdbcUtil {
    private static DataSource dataSource;

    private JdbcUtil() {
    }

    static {
        Properties properties = new Properties();
        String propertiesFile = "/mysql.properties";
        try {
            InputStream is = JdbcUtil.class.getResourceAsStream(propertiesFile);
            properties.load(is);
            dataSource = DruidDataSourceFactory.createDataSource(properties);
            log.info("加载数据库连接属性文件{}", propertiesFile);
        } catch (IOException | ClassNotFoundException e) {
            log.error("属性文件{}不存在", propertiesFile);
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>() {
        @Override
        protected Connection initialValue() {
            Connection conn = null;
            try {
                conn = dataSource.getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
                log.error("获取Connection失败");
            }
            return conn;
        }
    };

    /**
     * 获取连接
     *
     * @return
     */
    public static Connection getConnection() {
        if (threadLocal.get() != null) {
            return threadLocal.get();
        }
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            threadLocal.set(conn);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * 释放资源
     *
     * @param conn
     * @param stmt
     * @param rs
     */
    public static void release(Connection conn, Statement stmt, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
                log.info("释放ResultSet对象{}成功", rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            log.error("释放ResultSet对象{}失败", rs);
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                    log.info("释放Statement对象{}成功", stmt);
                }
            } catch (SQLException e) {
                e.printStackTrace();
                log.error("释放Statement对象{}失败", stmt);
            } finally {
                try {
                    if (conn != null) {
                        conn.close();
                        threadLocal.remove();
                        log.info("释放Connection对象{}成功", conn);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                    log.error("释放连接对象{}失败", conn);
                }
            }
        }
    }

    public static void main(String[] args) {
        for(int i =0;i<11;i++) {
            new Thread(()->{
                Connection conn = getConnection();
                System.out.println(conn);
               release(conn,null,null);
            }).start();
        }
    }
}

